// miniprogram/rember2/list/list.js
const api = require('../../utils/api');
const {myRequest} = require('../../utils/myRequest.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    listId: 1008002,
    classId: 1005000,
    classList:[],
    titleIndex:null,
    subId:null,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      listId: options.listId,
      classId: options.classId
    })
    myRequest({
      url: api.list,
      data: {
        categoryId: this.data.listId,
        page: "",
        size: ""
      },
      success:(res)=>{
        console.log(res);
        this.setData({
          // listId: options,
          list: res.data.data.goodsList
        });
      }
    })
    console.log(this.data.listId)
    myRequest({
      url:api.current,
      data:{
        id:this.data.classId
      },
      success:(res)=>{
        console.log(res);
        this.setData({
          classList: res.data.data.currentCategory.subCategoryList,
        })
      }
    })
  },
  //顶部导航点击事件
  click_item(e){
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    myRequest({
      url: api.list,
      data: {
        categoryId: id,
        page: "",
        size: ""
      },
      success: (res) => {
        console.log('导航条点击切换成功', res);
        this.setData({
          list: res.data.data.goodsList,
          titleIndex: index
        })
      }
    })
  },
  // 商品点击事件
  clickSub(e){
    let subId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/rember2/goodsDetail/goodsDetail?subid=${subId}`,
    })
  },
  // 到底刷新
  lower(){
    console.log('到底了');
    let page =this.data.page + 1; 
    this.setData({
      page:page
    })
    myRequest({
      url: api.list,
      data: {
        categoryId: this.data.listId,
        page: page,
        size: ""
      },
      success: (res) => {
        console.log(res);
        this.setData({
          // listId: options,
          list: this.data.list.concat(res.data.data.goodsList)
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var self = this;
    self.setData({
      subIndex: 1
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})